﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProxyWrapper;
using ProxyWrapper.UserProxy;

public partial class Register : System.Web.UI.Page
{
    public string messagetype = "";
    public string message = "";
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnCreateAccount_Click(object sender, EventArgs e)
    {

        try
        {
            AddUser();

        }
        catch (Exception ee)
        {

        }
    }

    private void AddUser()
    {
        bool isValidForm=true;
        UserServiceWrapper loginService = new UserServiceWrapper();
        string struserName = email.Text.Trim();
        string strPassword = password.Text.Trim();
        string strPassword2 = password2.Text.Trim();
        string firstName = txtFirstName.Text;
        string lastName = txtLastName.Text;
        string phoneNumber = txtPhoneNumber.Text;
        string strEmail = email.Text.Trim();
        string strOptionalCompany = company.Text.Trim();
       
        if(string.IsNullOrWhiteSpace(struserName))
        {
            isValidForm = false;
            message = "User Name is required";
        }
        else if (string.IsNullOrWhiteSpace(firstName))
        {
            isValidForm = false;
            message = "First Name is required";
        }
        else if (string.IsNullOrWhiteSpace(strEmail))
        {
            isValidForm = false;
            message = "Email is required";
        }
        else if (string.IsNullOrWhiteSpace(strPassword))
        {
            isValidForm = false;
            message = "Password is required";
        }
        else if (!strPassword.Equals(strPassword2))
        {
            isValidForm = false;
            message = "Password should match";
        }
        if (!isValidForm)
        {
            messagetype = "E"; 
        }
        if (isValidForm)
        {
            UserDetails objuDetails = new UserDetails() { UserId=0,
                UserName = struserName,
                FirstName= firstName,
                LastName = lastName,
                PhoneNumber= phoneNumber,
                EmailId = strEmail,
                Password= strPassword,
                Company = strOptionalCompany            
            };

            string result = loginService.RegisterUser(objuDetails);
            if (result.Trim() == "0")
            {
                ProxyWrapper.UserProxy.UserDetails obj = loginService.ValidateLogin(struserName, strPassword);
                // if (obj.IsUserValidated)
                /// {
                Session["UserObject"] = obj;
                Response.Redirect("FirstLogin.aspx");
                // }
                // else
                //  {
                //     showMessages.InnerHtml = UserMessages.SignUpFailed;
                //  }
            }
            else
            {
                messagetype = "E";
                message = result;
            }
        }
    }

    public string GetMessage()
    {
        string warning = "<div class='warning'><p></p><li>{0}</li><p></p></div>";
        string result = "<div class='success'><p></p><li>{0}</li><p></p></div>";
        if (messagetype == "E")
            return string.Format(warning, message);
        else if (messagetype == "S")
            return string.Format(result, message);
        else
            return "";

    }
}